package xuesm.proxy.dbroute;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: xuesm
 * @Description: OrderService的代理，只能代理OrderService
 * @Date: Create in 13:06 2019/4/2
 */
public class OrderServiceProxy implements IOrderService{

	private SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");

	private IOrderService orderService;
	public OrderServiceProxy(IOrderService service){
		this.orderService = service;
	}

	@Override
	public int insert(Order order) {
		
		before(order);
		orderService.insert(order);
		after();
		return 0;
	}

	private void after() {
		System.out.println("等待下一次代理");
	}

	private void before(Order order) {

		Long time = order.getCreateTime();
		int year = Integer.parseInt(yearFormat.format(new Date(time)));
		System.out.println("静态代理类自动分配到【DB_" + year + "】数据源处理数据");
		DynamicDataSourceEntry.set(year);
	}
}
